package com.changgou.goods.controller;

import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Spu;
import com.changgou.goods.service.SpuService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhengjd
 * @date 2020/5/5 21:49
 */
@RestController
@RequestMapping("spu")
public class SpuController {

    @Autowired
    private SpuService spuService;

    /**
     * 保存goods
     * @param goods
     * @return
     */
    @PostMapping("save")
    public Result save(@RequestBody Goods goods){
        spuService.save(goods);
        return new Result(true, StatusCode.OK,"保存成功");
    }

    /**
     * 根据spuId查询商品
     * @param id
     * @return
     */
    @GetMapping("goods/{id}")
    public Result<Goods> findGoodsById(@PathVariable Long id){
        Goods goods = spuService.findGoodsById(id);
        return new Result<Goods>(true,StatusCode.OK,"查询商品成功",goods);
    }

    /**
     * 商品审核
     * @param id
     * @return
     */
    @PutMapping("audit/{id}")
    public Result audit(@PathVariable(name = "id") Long id){
        spuService.audit(id);
        return new Result(true,StatusCode.OK,"商品审核成功");
    }

    /**
     * 商品下架
     * @param id
     * @return
     */
    @PutMapping("pull/{id}")
    public Result pull(@PathVariable(name = "id") Long id){
        spuService.pull(id);
        return new Result(true,StatusCode.OK,"商品下架成功");
    }

    /**
     * 商品上架
     * @param id
     * @return
     */
    @PutMapping("put/{id}")
    public Result put(@PathVariable(name = "id") Long id){
        spuService.put(id);
        return new Result(true,StatusCode.OK,"商品上架成功");
    }

    /**
     * 商品批量上架
     * @param ids
     * @return
     */
    @PutMapping("put/many")
    public Result put(@RequestBody Long[] ids){
        int count = spuService.putMany(ids);
        return new Result(true,StatusCode.OK,"商品批量上架"+count+"个成功");
    }

    /**
     * 商品批量下架
     * @param ids
     * @return
     */
    @PutMapping("pull/many")
    public Result pullMany(@RequestBody Long[] ids){
        int count = spuService.pullMany(ids);
        return new Result(true,StatusCode.OK,"商品批量下架"+count+"个成功");
    }

    /**
     * 商品逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping("logic/delete/{id}")
    public Result logicDelete(@PathVariable(name = "id") Long id){
        spuService.logicDelete(id);
        return new Result(true,StatusCode.OK,"商品逻辑删除成功");
    }

    /**
     * 还原被删除的商品
     * @param id
     * @return
     */
    @PutMapping("restore/{id}")
    public Result restore(@PathVariable(name = "id") Long id){
        spuService.restore(id);
        return new Result(true,StatusCode.OK,"商品还原成功");
    }

    /**
     * 物理删除商品
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable(name = "id") Long id){
        spuService.delete(id);
        return new Result(true,StatusCode.OK,"商品物理删除成功");
    }

    /***
     * Spu分页条件搜索实现
     * @param spu
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Spu>> findPage(@RequestBody(required = false)  Spu spu, @PathVariable  int page, @PathVariable  int size){
        //调用SpuService实现分页条件查询Spu
        PageInfo<Spu> pageInfo = spuService.findPage(spu, page, size);
        return new Result<PageInfo<Spu>>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 根据ID查询Spu数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Spu> findById(@PathVariable Long id){
        //调用SpuService实现根据主键查询Spu
        Spu spu = spuService.findById(id);
        return new Result<Spu>(true,StatusCode.OK,"查询成功",spu);
    }

    /***
     * 查询Spu全部数据
     * @return
     */
    @GetMapping
    public Result<List<Spu>> findAll(){
        //调用SpuService实现查询所有Spu
        List<Spu> list = spuService.findAll();
        return new Result<List<Spu>>(true, StatusCode.OK,"查询成功",list) ;
    }

}
